//搜索选择下拉
$.fn.filterSelect=(function(){
	    var isInit = false;
	    function initCss(){
	        isInit = true;
	        var style = document.createElement("style");
	        var csstext = '.m-input-select{display:inline-block;*display:inline;position:relative;-webkit-user-select:none;width:80%;}\
	            \n.m-input-select ul, .m-input-select li{padding:0;margin:0;}\
	            \n.m-input-select .m-input{width:100%;padding-right:22px;}\
	            \n.m-input-select .m-input-ico{position:absolute;right:0;top:0;width:22px;height:100%;background:url() no-repeat 50% 50%;}\
	            \n.m-input-select .m-list-wrapper{}\
	            \n.m-input-select .m-list{display:none;position:absolute;z-index:1;top:100%;left:0;right:0;max-width:100%;max-height:250px;overflow:auto;border-bottom:1px solid #ddd;}\
	            \n.m-input-select .m-list-item{cursor:default;padding:5px;margin-top:-1px;list-style:none;background:#fff;border:1px solid #ddd;border-bottom:none;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}\
	            \n.m-input-select .m-list-item:hover{background:#2D95FF;}\
	            \n.m-input-select .m-list-item-active{background:#2D95FF;}';
	        style = $("<style>"+ csstext +"</style>")[0];
	        var head = document.head || document.getElementsByTagName("head")[0];
	        if(head.hasChildNodes()){
	            head.insertBefore(style, head.firstChild);
	        }else{
	            head.appendChild(style);
	        }
	    }
	    return function(){
	        !isInit && initCss();
	        var $body = $("body");
	        this.each(function(i, v){
	            var $sel = $(v), $div = $('<div class="m-input-select"></div>');
	            var $input = $("<input type='text' class='m-input' />");
	            var $wrapper = $("<ul class='m-list"+"'></ul>");
	            $div = $sel.wrap($div).hide().addClass("m-select").parent();
	            $div.append($input).append("<span class='m-input-ico'></span>").append($wrapper);
	            // 遮罩层显示 + 隐藏
	            var wrapper = {
	                show: function(){
	                    $wrapper.show();
	                    this.$list = $wrapper.find(".m-list-item:visible");
	                    this.setIndex(this.$list.filter(".m-list-item-active"));
	                    this.setActive(this.index);
	                },
	                hide: function(){
	                    $wrapper.hide();
	                },
	                next: function(){
	                    return this.setActive(this.index + 1);
	                },
	                prev: function(){
	                    return this.setActive(this.index - 1);
	                },
	                $list: $wrapper.find(".m-list-item"),
	                index: 0,
	                $cur: [],
	                setActive: function(i){
	                    // 找到第1个 li，并且赋值为 active
	                    var $list = this.$list, size = $list.size();
	                    if(size <= 0){
	                        this.$cur = [];
	                        return;
	                    }
	                    $list.filter(".m-list-item-active").removeClass("m-list-item-active");
	                    if(i < 0){
	                        i = 0;
	                    }else if(i >= size){
	                        i = size - 1;
	                    }
	                    this.index = i;
	                    this.$cur = $list.eq(i).addClass("m-list-item-active");
	                    this.fixScroll(this.$cur);
	                    return this.$cur;
	                },
	                fixScroll: function($elem){
	                    // console.log($wrapper);
	                    var height = $wrapper.height(), top = $elem.position().top, eHeight = $elem.outerHeight();
	                    var scroll = $wrapper.scrollTop();
	                    // 因为 li 的 实际　top，应该要加上 滚上 的距离
	                    top += scroll;
	                    if(scroll > top){
	                        $wrapper.scrollTop(top);
	                    }else if(top + eHeight > scroll + height){
	                        // $wrapper.scrollTop(top + height - eHeight);
	                        $wrapper.scrollTop(top + eHeight - height);
	                    }
	                },
	                setIndex: function($li){
	                    if($li.size() > 0){
	                        this.index = this.$list.index($li);
	                        $li.addClass("m-list-item-active").siblings().removeClass("m-list-item-active");
	                    }else{
	                        this.index = 0;
	                    }
	                }
	            };
	            // input 的操作
	            var operation = {
	                // 文字更变了，更新 li, 最低效率的一种
	                textChange: function(){
	                    val = $.trim($input.val());
	                    $wrapper.find(".m-list-item").each(function(i, v){
	                        if(v.innerHTML.indexOf(val) >= 0){
	                            $(v).show();
	                        }else{
	                            $(v).hide();
	                        }
	                    });
	                    wrapper.show();
	                },
	                // 设值
	                setValue: function($li){
	                    if($li && $li.size() > 0){
	                        var val = $.trim($li.html());
	                        $input.val(val).attr("placeholder", val);
	                        wrapper.setIndex($li);
	                        $sel.val($li.attr("data-value")).trigger("change");
	                    }else{
	                        $input.val(function(i, v){
	                            return $input.attr("placeholder");
	                        });
	                    }
	                    wrapper.hide();
	                    this.offBody();
	                },
	                onBody: function(){
	                    var self = this;
	                    setTimeout(function(){
	                        self.offBody();
	                        $body.on("click", self.bodyClick);
	                    }, 10);
	                },
	                offBody: function(){
	                    $body.off("click", this.bodyClick);
	                },
	                bodyClick: function(e){
	                    var target = e.target;
	                    if(target != $input[0] && target != $wrapper[0]){
	                        wrapper.hide();
	                        operation.setValue();
	                        operation.offBody();
	                    }
	                }
	            };
	
	            // 遍历 $sel 对象
	            function resetOption(){
	                var html = "", val = "";
	                $sel.find("option").each(function(i, v){
	                    if(v.selected && !val){
	                        val = v.text;
	                    }
	                    html += '<li class="m-list-item'+ (v.selected ? " m-list-item-active" : "") +'" data-value="'+ v.value +'">'+ v.text +'</li>';
	                });
	                $input.val(val);
	                $wrapper.html(html);
	            }
	            $sel.on("optionChange", resetOption).trigger("optionChange");
	            $sel.on("setEditSelectValue", function(e, val){
	                // console.log(val);
	                var $all = $wrapper.find(".m-list-item"), $item;
	                for(var i = 0, max = $all.size(); i < max; i++){
	                    $item = $all.eq(i);
	                    if($item.attr("data-value") == val){
	                        operation.setValue($item);
	                        return;
	                    }
	                }
	            });
	            // input 聚焦
	            $input.on("focus", function(){
	                this.value = "";
	                operation.textChange();
	                operation.onBody();
	            }).on("input propertychange", function(e){
	                        operation.textChange();
	                    }).on("keydown", function(e){
	                        // 上 38, 下 40， enter 13
	                        switch(e.keyCode){
	                            case 38:
	                                wrapper.prev();
	                                break;
	                            case 40:
	                                wrapper.next();
	                                break;
	                            case 13:
	                                operation.setValue(wrapper.$cur);
	                                break;
	                        }
	                    });
	            $div.on("click", ".m-input-ico", function(){
	                $wrapper.is(":visible") ? $input.blur() : ($input.val("").trigger("focus"));
	            });
	            // 选中
	            $wrapper.on("click", ".m-list-item", function(){
	                operation.setValue($(this));
	                return false;
	            });
	            setTimeout(function(){
	                // for ie
	                wrapper.hide();
	            }, 1)
	        });
	        return this;
	    };
	}
)();

$("select[data-edit-select='1']").filterSelect();
/*$select.on("change", function(){
});*/


//搜索选择下拉
$.fn.filterSelect2=(function(){
	    var isInit = false;
	    function initCss(){
	        isInit = true;
	        var style = document.createElement("style");
	        var csstext = '.m-input-select{display:inline-block;*display:inline;position:relative;-webkit-user-select:none;width:80%;}\
	            \n.m-input-select ul, .m-input-select li{padding:0;margin:0;}\
	            \n.m-input-select .m-input{width:100%;padding-right:22px;}\
	            \n.m-input-select .m-input-ico{position:absolute;right:0;top:0;width:22px;height:100%;background:url() no-repeat 50% 50%;}\
	            \n.m-input-select .m-list-wrapper{}\
	            \n.m-input-select .m-list{display:none;position:absolute;z-index:1;top:100%;left:0;right:0;max-width:100%;max-height:250px;overflow:auto;border-bottom:1px solid #ddd;}\
	            \n.m-input-select .m-list-item{cursor:default;padding:5px;margin-top:-1px;list-style:none;background:#fff;border:1px solid #ddd;border-bottom:none;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}\
	            \n.m-input-select .m-list-item:hover{background:#2D95FF;}\
	            \n.m-input-select .m-list-item-active{background:#2D95FF;}';
	        style = $("<style>"+ csstext +"</style>")[0];
	        var head = document.head || document.getElementsByTagName("head")[0];
	        if(head.hasChildNodes()){
	            head.insertBefore(style, head.firstChild);
	        }else{
	            head.appendChild(style);
	        }
	    }
	    return function(){
	        !isInit && initCss();
	        var $body = $("body");
	        this.each(function(i, v){
	            var $sel = $(v), $div = $('<div class="m-input-select"></div>');
	            var $input = $("<input type='text' class='m-input' />");
	            var $wrapper = $("<ul class='m-list2"+"'></ul>");
	            $div = $sel.wrap($div).hide().addClass("m-select").parent();
	            $div.append($input).append("<span class='m-input-ico'></span>").append($wrapper);
	            // 遮罩层显示 + 隐藏
	            var wrapper = {
	                show: function(){
	                    $wrapper.show();
	                    this.$list = $wrapper.find(".m-list-item:visible");
	                    this.setIndex(this.$list.filter(".m-list-item-active"));
	                    this.setActive(this.index);
	                },
	                hide: function(){
	                    $wrapper.hide();
	                },
	                next: function(){
	                    return this.setActive(this.index + 1);
	                },
	                prev: function(){
	                    return this.setActive(this.index - 1);
	                },
	                $list: $wrapper.find(".m-list-item"),
	                index: 0,
	                $cur: [],
	                setActive: function(i){
	                    // 找到第1个 li，并且赋值为 active
	                    var $list = this.$list, size = $list.size();
	                    if(size <= 0){
	                        this.$cur = [];
	                        return;
	                    }
	                    $list.filter(".m-list-item-active").removeClass("m-list-item-active");
	                    if(i < 0){
	                        i = 0;
	                    }else if(i >= size){
	                        i = size - 1;
	                    }
	                    this.index = i;
	                    this.$cur = $list.eq(i).addClass("m-list-item-active");
	                    this.fixScroll(this.$cur);
	                    return this.$cur;
	                },
	                fixScroll: function($elem){
	                    // console.log($wrapper);
	                    var height = $wrapper.height(), top = $elem.position().top, eHeight = $elem.outerHeight();
	                    var scroll = $wrapper.scrollTop();
	                    // 因为 li 的 实际　top，应该要加上 滚上 的距离
	                    top += scroll;
	                    if(scroll > top){
	                        $wrapper.scrollTop(top);
	                    }else if(top + eHeight > scroll + height){
	                        // $wrapper.scrollTop(top + height - eHeight);
	                        $wrapper.scrollTop(top + eHeight - height);
	                    }
	                },
	                setIndex: function($li){
	                    if($li.size() > 0){
	                        this.index = this.$list.index($li);
	                        $li.addClass("m-list-item-active").siblings().removeClass("m-list-item-active");
	                    }else{
	                        this.index = 0;
	                    }
	                }
	            };
	            // input 的操作
	            var operation = {
	                // 文字更变了，更新 li, 最低效率的一种
	                textChange: function(){
	                    val = $.trim($input.val());
	                    $wrapper.find(".m-list-item").each(function(i, v){
	                        if(v.innerHTML.indexOf(val) >= 0){
	                            $(v).show();
	                        }else{
	                            $(v).hide();
	                        }
	                    });
	                    wrapper.show();
	                },
	                // 设值
	                setValue: function($li){
	                    if($li && $li.size() > 0){
	                        var val = $.trim($li.html());
	                        $input.val(val).attr("placeholder", val);
	                        wrapper.setIndex($li);
	                        $sel.val($li.attr("data-value")).trigger("change");
	                    }else{
	                        $input.val(function(i, v){
	                            return $input.attr("placeholder");
	                        });
	                    }
	                    wrapper.hide();
	                    this.offBody();
	                },
	                onBody: function(){
	                    var self = this;
	                    setTimeout(function(){
	                        self.offBody();
	                        $body.on("click", self.bodyClick);
	                    }, 10);
	                },
	                offBody: function(){
	                    $body.off("click", this.bodyClick);
	                },
	                bodyClick: function(e){
	                    var target = e.target;
	                    if(target != $input[0] && target != $wrapper[0]){
	                        wrapper.hide();
	                        operation.setValue();
	                        operation.offBody();
	                    }
	                }
	            };
	
	            // 遍历 $sel 对象
	            function resetOption(){
	                var html = "", val = "";
	                $sel.find("option").each(function(i, v){
	                    if(v.selected && !val){
	                        val = v.text;
	                    }
	                    html += '<li class="m-list-item'+ (v.selected ? " m-list-item-active" : "") +'" data-value="'+ v.value +'">'+ v.text +'</li>';
	                });
	                $input.val(val);
	                $wrapper.html(html);
	            }
	            $sel.on("optionChange", resetOption).trigger("optionChange");
	            $sel.on("setEditSelectValue", function(e, val){
	                // console.log(val);
	                var $all = $wrapper.find(".m-list-item"), $item;
	                for(var i = 0, max = $all.size(); i < max; i++){
	                    $item = $all.eq(i);
	                    if($item.attr("data-value") == val){
	                        operation.setValue($item);
	                        return;
	                    }
	                }
	            });
	            // input 聚焦
	            $input.on("focus", function(){
	                this.value = "";
	                operation.textChange();
	                operation.onBody();
	            }).on("input propertychange", function(e){
	                        operation.textChange();
	                    }).on("keydown", function(e){
	                        // 上 38, 下 40， enter 13
	                        switch(e.keyCode){
	                            case 38:
	                                wrapper.prev();
	                                break;
	                            case 40:
	                                wrapper.next();
	                                break;
	                            case 13:
	                                operation.setValue(wrapper.$cur);
	                                break;
	                        }
	                    });
	            $div.on("click", ".m-input-ico", function(){
	                $wrapper.is(":visible") ? $input.blur() : ($input.val("").trigger("focus"));
	            });
	            // 选中
	            $wrapper.on("click", ".m-list-item", function(){
	                operation.setValue($(this));
	                return false;
	            });
	            setTimeout(function(){
	                // for ie
	                wrapper.hide();
	            }, 1)
	        });
	        return this;
	    };
	}
)();

$("select[data-edit-select='2']").filterSelect2();




